package com.pxl.bi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pxl.bi.common.ErrorCode;
import com.pxl.bi.exception.BusinessException;
import com.pxl.bi.exception.ThrowUtils;
import com.pxl.bi.mapper.ChartMapper;
import com.pxl.bi.model.dto.chart.ChartQueryRequest;
import com.pxl.bi.model.entity.Chart;
import com.pxl.bi.model.enums.ChartStatusEnum;
import com.pxl.bi.service.ChartService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 彭显良
* @description 针对表【chart(图表信息表)】的数据库操作Service实现
* @createDate 2024-03-11 20:53:45
*/
@Service
public class ChartServiceImpl extends ServiceImpl<ChartMapper, Chart>
    implements ChartService {
    @Resource
    private ChartMapper chartMapper;

    @Override
    public void validChart(Chart chart, boolean add) {
        if (chart == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 创建时，参数不能为空
        if (add) {
//            ThrowUtils.throwIf(StringUtils.isAnyBlank(title, content, tags), ErrorCode.PARAMS_ERROR);
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
    }

    @Override
    public boolean addChartData(Chart chart) {
        return  chartMapper.insertChartData(chart);
    }

    @Override
    public Page<Chart> listChartByPage(ChartQueryRequest chartQueryRequest) {
        long current = chartQueryRequest.getCurrent();
        long size = chartQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        // 查询总记录数
        QueryWrapper<Chart> chartQueryWrapper = new QueryWrapper<>();
        Long userId = chartQueryRequest.getUserId();
        chartQueryWrapper.eq(ObjectUtils.isNotEmpty(userId),"userId",userId);
        long total = this.count(chartQueryWrapper);
        // 计算偏移量
        long offset = (current - 1) * size;
        List<Chart> chartList = chartMapper.pageChartData(size, offset);

        //封装Page
        Page<Chart> page = new Page<>();
        page.setCurrent(current);
        page.setSize(size);
        page.setTotal(total);
        page.setRecords(chartList);
        return page;
    }

    @Override
    public String getChartDataById(long chartId) {
        return chartMapper.getChartDataById(chartId);
    }

    /**
     * 处理更改图表异常
     * @param chartId
     * @param execMessage
     */
    public void handleChartUpdateError(long chartId, String execMessage) {
        Chart updateChartResult = new Chart();
        updateChartResult.setId(chartId);
        updateChartResult.setStatus(ChartStatusEnum.FAILED.getValue());
        updateChartResult.setExecMessage(execMessage);
        boolean updateResult = this.updateById(updateChartResult);
        if (!updateResult) {
            log.error("更新图表失败状态失败" + chartId + "," + execMessage);
        }
    }
}




